package fw.asmx.appstoper.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import fw.asmx.appstoper.MainActivity;
import fw.asmx.appstoper.R;
import fw.asmx.appstoper.adapter.AppListAdapter;
import fw.asmx.appstoper.adapter.item.AppItem;
import fw.asmx.appstoper.utils.SPOperat;

public class DataAppFragment extends Fragment
        implements AppListAdapter.OnItemClickListener
{
    private TextView tv;
    private RecyclerView rv;

    private MainActivity activity;

    private AppListAdapter adapter;

    public DataAppFragment()
    {
    }

    @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                 Bundle savedInstanceState)
    {
        activity = (MainActivity) getActivity();
        View v = inflater.inflate(R.layout.fragment_list, container, false);

        tv = (TextView) v.findViewById(R.id.tv_empty_view);
        rv = (RecyclerView) v.findViewById(R.id.rv);

        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).build());

        adapter=new AppListAdapter(activity,activity.Datapps);
        adapter.setOnItemClickListener(this);
        rv.setAdapter(adapter);
        return v;
    }

    @Override public void onItemClick(View v, AppItem ai, int pos)
    {
        AppListAdapter.ViewHolder vh = (AppListAdapter.ViewHolder) v.getTag();
        vh.cb.setChecked(!vh.cb.isChecked());
        activity.chkedMap.put(ai.pkgName, vh.cb.isChecked());
        SPOperat.writeSPBool(getActivity(), ai.pkgName, vh.cb.isChecked());
    }

    public void freshList()
    {
        adapter.notifyDataSetChanged();
        tv.setVisibility(View.GONE);
        rv.setVisibility(View.VISIBLE);
    }
}
